import webview
import json
import os
import shutil

class charEditAPI():
    def get_char_data(self):
        with open("char/Kazuha/char_inf.json","r",encoding="utf-8")as f:
            data = json.load(f)
        return data
    def file_select(self,f_type):
        base_path ={
            "image":"char/Kazuha/",
            "audio":"char/Kazuha/voice/"
        }
        data = window.create_file_dialog(webview.OPEN_DIALOG)
        if data == None:
            return {"success":False,"msg":"用户取消"}
        else:
            data = data[0]
            local_path=base_path[f_type]+os.path.basename(data)
            if os.path.exists(local_path):
                os.remove(local_path)
            shutil.copy(data,local_path)
            return {"success":True,"data":local_path}
    def save(self,charD):
        with open("char/Kazuha/char_inf.json","w",encoding="utf-8")as f:
            json.dump(charD,f)
    def writeCSS(self,css):
        with open("char/Kazuha/plug.css","w",encoding="utf-8")as f:
            f.write(css)
api=charEditAPI()
window = webview.create_window(
    '梦幻空间',
    "char_edit.html",
    width=1300,
    height=600,
    text_select=False,
    confirm_close=False,
    draggable=False,
    js_api=api,
    easy_drag=False
)
webview.start()